﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Configuration;
using System.Drawing.Imaging;
using System.Drawing.Text;


namespace WaterMarker
{
    public partial class WaterMarker : Form
    {
        private static string[] fileNames = null;
        private static string destinationFolder = null;
        public static string waterMarkString = string.Empty;
        public static float x = 0;
        public static float y = 0;

        public WaterMarker()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            //Color color = System.Drawing.Color.Blue;
            //Pen pen=new System.Drawing.Pen(new System.Drawing.SolidBrush(color));
            //Font boldFont = new System.Drawing.Font("Arial", 15);
            //Brush brush=new SolidBrush(color);

            //Image img = Image.FromFile(@"c:\1.png");
            //Graphics container = Graphics.FromImage(img);
            //int width = img.Width;
            //int height = img.Height;
            //PointF startPosition = new PointF(width / 2 / 2, height - 40);

            //container.DrawString("博客地址 http://blog.csdn.net/ghostbear", boldFont, brush, startPosition);

            //container.Flush();
            
          
            //img.Save(@"c:\1_bak.png");
            //container.Save();
            

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btnChooseFiles_Click(object sender, EventArgs e)
        {
            opdSelectFiles.ShowDialog();
            fileNames = opdSelectFiles.FileNames;
            FormatFileName();

        }

        private void FormatFileName()
        {
            if (fileNames == null)
            {
                return;
            }
            foreach (string name in fileNames)
            {
                rtbFiles.Text += Path.GetFileName(name) + "\n";
            }
        }

        private void btnChooseSaveFolder_Click(object sender, EventArgs e)
        {
            fbdDestinationFolder.ShowDialog();
            destinationFolder=txtDestinationFolder.Text = fbdDestinationFolder.SelectedPath;
        }



        private void btnGO_Click(object sender, EventArgs e)
        {
            if (fileNames != null && string.IsNullOrWhiteSpace(txtDestinationFolder.Text) == false)
            {
                Image img = null;

                foreach (string file in fileNames)
                {
                    try
                    {
                        img = MarkImage(file);
                        img.Save(destinationFolder + "\\" + Path.GetFileName(file));
                        img.Dispose();
                        rtbFiles.Text = rtbFiles.Text.Replace(Path.GetFileName(file), Path.GetFileName(file) + " ok!");
                    }
                    catch
                    {
                        rtbFiles.Text.Replace(Path.GetFileName(file), Path.GetFileName(file) + "fail!");
                    }


                }
            }
        }

        public static Image MarkImage(string file)
        {
            Color color = System.Drawing.Color.Blue;
            Pen pen = new System.Drawing.Pen(new System.Drawing.SolidBrush(color));
            Font boldFont = new System.Drawing.Font("Arial", 15);
            Brush brush = new SolidBrush(color);
            PointF startPosition;
            Graphics container;
            Image img;
            int width = 0;
            int height = 0;

            img = Image.FromFile(file);
            
                using (container = Graphics.FromImage(img))
                {
                    width = img.Width;
                    height = img.Height;
                    startPosition = new PointF(width / 2 / 2+x, height+y);
                    container.DrawString(waterMarkString, boldFont, brush, startPosition);
                    return img;
                }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Options optionsForm = new Options();
            optionsForm.Show();
        }
    }
}
